package com.baoyi.service;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.baoyi.content.content;
import com.baoyi.doamin.ImageItem;
import com.baoyi.doamin.NewData;
import com.baoyi.utils.Connection;
import com.baoyi.utils.HttpConnection;
import com.baoyi.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ImageItemService {
	private static Map<String, String> caches;
	static {
		caches = new HashMap<String, String>();
	}

	public static List<ImageItem> finPailu() {
		String url = content.server + "PaiKuList";
		String key = Utils.getMD5Str(url);
		String messages = null;

		messages = caches.get(key);
		if (null != messages) {

		} else {
			Connection conn = HttpConnection.connect(url);
			try {
				messages = conn.execute().body();
				caches.put(key, messages);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

		Gson gson = new Gson();
		Type listType = new TypeToken<LinkedList<ImageItem>>() {
		}.getType();
		List<ImageItem> items = null;
		items = gson.fromJson(messages, listType);
		return items;
	}

	public static List<ImageItem> find(String type) {
		String url = content.server + "WeekItems";
		String key = Utils.getMD5Str(url)+type;
		String messages = null;
		messages = caches.get(key);
		if (null != messages) {

		} else {
			Connection conn = HttpConnection.connect(url);
			conn.data("type", type);
			try {
				messages = conn.execute().body();
				caches.put(key, messages);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		Gson gson = new Gson();
		Type listType = new TypeToken<LinkedList<ImageItem>>() {
		}.getType();
		List<ImageItem> items = null;
		items = gson.fromJson(messages, listType);
		return items;
	}

	public static List<ImageItem> page(Integer curpage) {
		List<ImageItem> items = new ArrayList<ImageItem>();
		String url = content.server + "PageIndex";
		Connection conn = HttpConnection.connect(url);
		conn.data("curpage", curpage.toString());
		String messages = null;
		try {
			messages = conn.execute().body();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Gson gson = new Gson();
		Type listType = new TypeToken<LinkedList<ImageItem>>() {
		}.getType();
		items = gson.fromJson(messages, listType);
		return items;
	}

	public static NewData FindNewIndex() {
		String url = content.server + "FindNewIndex";
		String key = Utils.getMD5Str(url);
		String messages = null;
		messages = caches.get(key);
		if (null != messages) {

		} else {
			Connection conn = HttpConnection.connect(url);
			try {
				messages = conn.execute().body();
				caches.put(key, messages);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		NewData items = new NewData();
		Gson gson = new Gson();
		items = gson.fromJson(messages, NewData.class);
		return items;
	}

	public static ImageItem findById(Integer id) {
		ImageItem items = new ImageItem();
		String url = content.server + "OneImage";
		Connection conn = HttpConnection.connect(url);
		conn.data("id", id.toString());
		String messages = null;
		try {
			messages = conn.execute().body();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Gson gson = new Gson();
		items = gson.fromJson(messages, ImageItem.class);
		return items;
	}

	public static ImageItem ding(Integer id) {
		ImageItem items = new ImageItem();
		String url = content.server + "Ding";
		Connection conn = HttpConnection.connect(url);
		conn.data("id", id.toString());
		String messages = null;
		try {
			messages = conn.execute().body();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Gson gson = new Gson();
		items = gson.fromJson(messages, ImageItem.class);
		return items;
	}

	public static ImageItem bishi(Integer id) {
		ImageItem items = new ImageItem();
		String url = content.server + "BiShi";
		Connection conn = HttpConnection.connect(url);
		conn.data("id", id.toString());
		String messages = null;
		try {
			messages = conn.execute().body();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Gson gson = new Gson();
		items = gson.fromJson(messages, ImageItem.class);
		return items;
	}

	public static ImageItem gaoxiao(Integer id) {
		ImageItem items = new ImageItem();
		String url = content.server + "GaoXiao";
		Connection conn = HttpConnection.connect(url);
		conn.data("id", id.toString());
		String messages = null;
		try {
			messages = conn.execute().body();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Gson gson = new Gson();
		items = gson.fromJson(messages, ImageItem.class);
		return items;
	}

	public static ImageItem xinqi(Integer id) {
		ImageItem items = new ImageItem();
		String url = content.server + "XinQi";
		Connection conn = HttpConnection.connect(url);
		conn.data("id", id.toString());
		String messages = null;
		try {
			messages = conn.execute().body();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		Gson gson = new Gson();
		items = gson.fromJson(messages, ImageItem.class);
		return items;
	}
}
